package com.weu.contorller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weu.bean.*;
import com.weu.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SaleController {

    @Resource
    private ISaleService iSaleService;

    @Resource
    private IModelService iModelService;

    @Resource
    private ISeriesService iSeriesService;

    @Resource
    private ICarTypeService iCarTypeService;

    @Resource
    private IBrandService iBrandService;

    /**
     * 查询经销商下所有出售的车辆
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/sales")
    public String getAll(@RequestParam(value = "userId", defaultValue = "5") Integer userId,
                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                         Model model) {
        //为了程序的严谨性，判断非空：
        if (pageNum == null) {
            pageNum = 1;   //设置默认当前页
        }
        if (pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;    //设置默认每页显示的数据数
        }
        System.out.println("当前页是：" + pageNum + "显示条数是：" + pageSize);

        PageHelper.startPage(pageNum, pageSize);
        try {
            List<Car> list = iSaleService.getByUid(userId);
            List<CarModel> modelList = new ArrayList<CarModel>();
            for(int i=0;i<list.size();i++){
                int modelId = list.get(i).getCarModelid();
                modelList.add(iModelService.getOne(modelId));
            }
            List<Series>seriesList = new ArrayList<>();
            for(int i=0;i<modelList.size();i++){
                int seriesId = modelList.get(i).getModelSeriesid();
                seriesList.add(iSeriesService.getOne(seriesId));
            }
            List<CarType>carTypeList = new ArrayList<>();
            for(int i=0;i<modelList.size();i++){
                int carTypeId = seriesList.get(i).getSeriesCartypeid();
                carTypeList.add(iCarTypeService.getOne(carTypeId));
            }
            List<Brand> brandList = new ArrayList<>();
            for(int i=0;i<seriesList.size();i++){
                int brandId = seriesList.get(i).getSeriesBrandid();
                brandList.add(iBrandService.getOne(brandId));
            }
            System.out.println("分页数据：" + list);
            PageInfo<Car> pageInfo = new PageInfo<Car>(list, pageSize);
            model.addAttribute("pageInfo", pageInfo);
            model.addAttribute("modelList", modelList);
            model.addAttribute("seriesList", seriesList);
            model.addAttribute("carTypeList", carTypeList);
            model.addAttribute("brandList", brandList);

        } finally {
            PageHelper.clearPage();
        }
        //  型号信息
//        List<CarModel> carModelList = iModelService.getAll();
//        model.addAttribute("carModelList", carModelList);

        //5.设置返回的jsp/html等前端页面
        return "/backstage/sale/list";
    }
}
